package org.luxor.commons.workflow.entity;

import org.apache.commons.lang.StringUtils;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 普通任务加签
 *
 * @author Mr.yan  @date 2021/10/19
 */
public class AddTaskInstanceExecution {

    /**
     * 任务ID
     */
    @NotBlank(message = "任务ID(taskId)，不能为空")
    private String taskId;

    /**
     * 操作原因
     */
    private String reason;

    /**
     * 目标任务-受理人
     */
    private String assignee;

    /**
     * 目标任务-候选人
     */
    private Candidate candidate;

    /**
     * 目标任务-到期时间
     */
    private Date dueDate;

    /**
     * 目标任务-优先级
     */
    private Integer priority;

    /**
     * 流程变量
     */
    private Map<String, Object> variables;
    /**
     * 瞬时变量
     */
    private Map<String, Object> transientVariables;

    public AddTaskInstanceExecution() {
        this.candidate = new Candidate();
        this.variables = new HashMap<>();
        this.transientVariables = new HashMap<>();
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getAssignee() {
        return assignee;
    }

    public void setAssignee(String targetAssignee) {
        this.assignee = targetAssignee;
    }

    public Candidate getCandidate() {
        return candidate;
    }

    public void setCandidate(Candidate candidate) {
        if (candidate == null) {
            this.candidate = new Candidate();
        } else {
            this.candidate = candidate;
        }
    }

    public void addCandidateUser(String userId) {
        this.candidate.addUser(userId);
    }

    public void addCandidateGroup(String groupId) {
        this.candidate.addGroup(groupId);
    }

    public Date getDueDate() {
        return dueDate;
    }

    public void setDueDate(Date dueDate) {
        this.dueDate = dueDate;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    public Map<String, Object> getVariables() {
        return variables;
    }

    public void setVariables(Map<String, Object> variables) {
        if (variables == null) {
            this.variables = new HashMap<>();
        } else {
            this.variables = variables;
        }
    }

    public void addVariable(String key, Object value) {
        if (StringUtils.isNotBlank(key)) {
            this.variables.put(key, value);
        }
    }

    public void removeVariable(String key) {
        if (StringUtils.isNotBlank(key)) {
            this.variables.remove(key);
        }
    }

    public Map<String, Object> getTransientVariables() {
        return transientVariables;
    }

    public void setTransientVariables(Map<String, Object> transientVariables) {
        if (transientVariables == null) {
            this.transientVariables = new HashMap<>();
        } else {
            this.transientVariables = transientVariables;
        }
    }

    public void addTransientVariable(String key, Object value) {
        if (StringUtils.isNotBlank(key)) {
            this.transientVariables.put(key, value);
        }
    }

    public void removeTransientVariable(String key) {
        if (StringUtils.isNotBlank(key)) {
            this.transientVariables.remove(key);
        }
    }

    @Override
    public String toString() {
        return "AddTaskInstanceExecution{" +
                "taskId='" + taskId + '\'' +
                ", reason='" + reason + '\'' +
                ", assignee='" + assignee + '\'' +
                ", candidate=" + candidate +
                ", dueDate=" + dueDate +
                ", priority=" + priority +
                ", variables=" + variables +
                ", transientVariables=" + transientVariables +
                '}';
    }
}
